import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import MoveCubePos from "./MoveCubePos";
import Sound from "../../../../constant/SoundNameConstant";
import { utils } from "../../../../utils/CommonUtil";
import EnumData from "../../../../Enum/EnumData";

export default class MoveCube extends InteractiveBase {
    /** 当前占的位置*/
    public curMoveCubePos: Array<MoveCubePos> = new Array<MoveCubePos>();
    //父
    public MoveCubeLock: any;

    private curMaterial:Laya.BlinnPhongMaterial;

    /** 初始材质颜色*/
    private originMaterialColor:Laya.Vector4;
    /** 选中材质颜色*/
    public selectColor:Laya.Vector4 = new Laya.Vector4(0.1751,1,0,0);
    /** 最终开启结束位置*/
    public endPos: MoveCubePos = new MoveCubePos(0,0);
    private mowner: any;
    //是否转换unity坐标到laya
    public isConvertUnityPosition: boolean = false;

    onStart() {
        if(this.owner.numChildren > 0){
            this.mowner = this.owner as Laya.Sprite3D;
            this.curMaterial = (this.owner.getChildAt(0) as Laya.MeshSprite3D).meshRenderer.material as Laya.BlinnPhongMaterial;
        }else{
            this.mowner = this.owner as Laya.MeshSprite3D;
            this.curMaterial = this.mowner.meshRenderer.material as Laya.BlinnPhongMaterial;
        }
        
       this.originMaterialColor = this.curMaterial.albedoColor;
    }

    protected InteractiveLogic(): boolean {
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.BUTTON);
        /** 点击了方块*/
        /** 当前选中的是自己  就取消选中*/
        if (this.MoveCubeLock.curSelectCube == this) {
            this.MoveCubeLock.SetSelectCube(null);
        }
        else {
            this.MoveCubeLock.SetSelectCube(this);
        }
        return false;
    }
    /** 设置是否选中状态*/
    public SetSelect(isSelect: boolean) {
        if (isSelect) {
           this.curMaterial.albedoColor = this.selectColor;
        }
        else {
              this.curMaterial.albedoColor = this.originMaterialColor;
        }
    }
    /** 当前是否占着某个位置*/
    public IsStandPos(row: number, col: number): boolean {
        for (let index = 0; index < this.curMoveCubePos.length; index++) {
            let item = this.curMoveCubePos[index];
            if (item.Row == row && item.col == col) {
                return true;
            }
        }
        return false;
    }
    /** 移动*/
    public Move(moveDir: EnumData.MoveCubeDirEnum) {
        /** 目标位置*/
        let targetPos: Laya.Vector3 = new Laya.Vector3(0, 0, 0);
        if (this.curMoveCubePos.length == 1) {
            targetPos = this.MoveCubeLock.GetPosByDir(this.curMoveCubePos[0].Row, this.curMoveCubePos[0].col, moveDir);
        }
        if (this.curMoveCubePos.length == 2) {
            let target1: Laya.Vector3 = this.MoveCubeLock.GetPosByDir(this.curMoveCubePos[0].Row, this.curMoveCubePos[0].col, moveDir);
            let target2: Laya.Vector3 = this.MoveCubeLock.GetPosByDir(this.curMoveCubePos[1].Row, this.curMoveCubePos[1].col, moveDir);
            Laya.Vector3.add(target1, target2, targetPos);
            targetPos = new Laya.Vector3(targetPos.x / 2, targetPos.y / 2, targetPos.z / 2);
        }
        this.MoveCubeLock.SetIsMoving(true);
        /** 目标点占据的位置*/
        window.gameFacade.SoundMng.playSound(Sound.soundClipArr.MOVE);

        /** 移动*/
        utils.CommonUtils.DOPositionTween(
            this.mowner.transform,
            this.mowner.transform.position,
            targetPos, 300,
            Laya.Handler.create(this, function () {
                console.log("移动到了目标位置了", targetPos);
                this.MoveCubeLock.SetIsMoving(false);
                /** 改变当前占据的位置*/
                this.curMoveCubePos = this.MoveCubeLock.GetMovePosByDir(this.curMoveCubePos, moveDir);
                /** 检测是否过到达最终位置了*/
                if (this.curMoveCubePos.length == 1) {
                    let curPos: MoveCubePos = this.curMoveCubePos[0];
                    if (this.endPos.Row == curPos.Row && this.endPos.col == curPos.col) {
                        this.MoveCubeLock.SetFinish();
                    }
                }

            }), false, false, this.isConvertUnityPosition)
    }
}